import { View, Text, Button } from '@tarojs/components'
import Taro, { onAppShow } from '@tarojs/taro';
import React, { Component, useState } from 'react';

const Home = () => {
  // [{nickName:undefined,avatarUrl:undefined}]
  const [user, setUser] = useState < any > ([])

  const getUser = () => {
    // const that = this
    const ui = Taro.getStorageSync('openid')
    console.log('UI:' + ui)
    console.log(typeof ui)
    // 通过判断登录时进入缓存openid是否存在
    if (!ui) {
      Taro.switchTab({
        url: '/pages/index/index'
      })
    } else {
      Taro.cloud.callFunction({
        name: 'home',
        data: {
          openid: ui
        }
      }).then((res) => {
        console.log(res.result)
        if (res.result) {
          setUser(res.result.data)
        }
      })

    }
  }
  return (
    <View>
      <View>
        <Button onClick={getUser}> 点击获取用户的信息</Button>
        <View>
          {user.map((item) => (
            <View>{item.nickName}</View>

          ))}
        </View>
      </View>

    </View>
  )

}
export default Home
